package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.common.domain.info.PlanInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.PlanRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.PlanService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:31
 */
@Api("")
@RestController
@RequestMapping("/plan")
public class PlanController extends BaseController {


  @Resource
  private PlanService planService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('planManage:list')")
  @GetMapping("/getPlanByPage")
  public TableDataInfo getPlanByPage(PlanRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(planService.getPlanByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('planManage:option')")
  @GetMapping("/getPlanForOption")
  public TableDataInfo getPlanForOption(PlanRequestDTO request) {
    return getDataTable(planService.getPlanForOption(request));
  }

  @Log(title = "计划管理", businessType = BusinessType.INSERT)
  @PostMapping("/addPlan")
  public AjaxResult addPlan(@RequestBody PlanRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    planService.addPlan(request);
    return success();
  }

  @Log(title = "计划管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyPlan")
  public AjaxResult modifyPlan(@RequestBody PlanRequestDTO request) {
    checkService.check("data_plan", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    planService.modifyPlan(request);
    return success();
  }

  @Log(title = "计划管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deletePlan/{planCode}")
  public AjaxResult deletePlan(@PathVariable String planCode) {
    checkService.check("data_plan", planCode);
    planService.deletePlan(planCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

  @GetMapping("/getPlanByCode/{planCode}")
  @PreAuthorize("@ss.hasPermi('system:plan:edit')")
  public AjaxResult getPlanDetailByCode(@PathVariable String planCode) {
    PlanInfo planInfo = planService.getPlanDetailByCode(planCode);
    return success(planInfo);
  }
}
